package com.xy.job.admin.repository

import com.xy.job.admin.domain.JobInfo
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import java.time.LocalDateTime

interface JobInfoRepository : JpaRepository<JobInfo, Long> {

    @Query(
        """
        select 
            distinct jd.lockName
        from com.xy.job.admin.domain.JobInfo jd
        where enabled = true
    """
    )
    fun findLockNameFromEnableJob(): List<String>

    @Query(
        """
        select 
            jd
        from com.xy.job.admin.domain.JobInfo jd
        where enabled = true
          and lockName in :lockNames
          and nextTriggerTime is not null 
          and nextTriggerTime <= :nextTriggerTime
    """
    )
    fun findAllScheduleable(lockNames: Iterable<String>, nextTriggerTime: LocalDateTime): Iterable<JobInfo>
}